package course;

import course.shared.PMF;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.jdo.PersistenceManager;
import department.Department;

/**
 * Servlet implementation class AddCourse
 */

public class addCourseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String nextJSP = "/ma_course.jsp";

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		String name = request.getParameter("coursename");
		String sem = request.getParameter("coursesem");
		Long dep = Long.parseLong(request.getParameter("department"));
		Long teacher = Long.parseLong(request.getParameter("teachername"));
		String outline = request.getParameter("coursedetail");
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			Department d = pm.getObjectById(Department.class,dep);
			d.addCourse(name,sem,d,teacher,outline);
		} finally {
		    pm.close();
		}
		
		response.sendRedirect(nextJSP);
		return;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
